package com.wx.day06.Test1.Param;

import com.wx.day06.Person;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@ResponseBody//直接返回字符串
@org.springframework.stereotype.Controller
@RequestMapping("user/param")

public class Controller {
   // 1. 直接接收 /param/data? account = root&age = 18;
    @GetMapping("data")
    public String paramData(String account, Integer age){ //int 基本数据类型不能null值 所以参数最好用包装类型
        return account + " " + age;
    }

    // 2. 使用@RequestParam注解  使用场景： 1.参数名和形参名不同 2. 约定必须传递参数@RequestParam(required = true) 3. 不传递设置默认值@RequestParam(defaultValue = "male")
    @GetMapping("data1")

    public String paramData1(@RequestParam(name = "username") String account, Integer age, @RequestParam(defaultValue = "male", required = false) String gender){
        return account + " " + age;
    }

    //3. 使用实体类接收参数 在形参列表中声明一个对应类型的对象即可
    @GetMapping("data2")
    public String paramData2(Person person){
        return person.toString();
    }

    //4. 一名多值  /....?list = ? & list = ? 形参要与参数名字对应
    @GetMapping("data3")
    public String paramData3(@RequestParam List<String> list){
        return list.toString();
    }
}
